<?php
require_once(dPgetConfig('root_dir') . "/modules/tasks/tasks.class.php");

require_once("mantis_config.php");

$forward_link = "";
if($_POST["project_id"] == "" || $_POST["project_id"] == 0){
	$forward_link= "m=mantis" ;
}
else{
	$forward_link = "m=projects&a=view&project_id=" . $_POST["project_id"];
}	
	
if(count($_POST["bugs"]) == 0 || $_POST["bug_action"] == ""){	
	$AppUI->redirect($forward_link);
	die();
}

if(($_POST["project_id"] == "" || $_POST["project_id"] == 0) && $_POST["bug_action"] == "task"){
	$AppUI->redirect($forward_link);
	die();
}

$mantis = new PHPXMLRPCClient();

$mantis->createClient($web_path_to_mantis_xmlrpc . "mantisserver.php", $domain);

$mantis->setXMLRPCDebug(0);

	
if($_POST["bug_action"] == "task"){
		
	foreach($_POST["bugs"] as $b){
		
		$mantis->resetRequest();
		$mantis->setFunction("MantisRPC");
		$mantis->addArg(array($mantis_user, $mantis_pass));
		$mantis->addArg("getMantisBugById");
		$mantis->addArg($b);

		$bug = $mantis->call();
	
		if(ERROR::isError($bug)){
			die($bug->getErrstr());
		}

		
		$obj = new CTask();
		$obj->task_id = false;
		$obj->task_name = $bug["summary"];
		$obj->task_project = $_POST["project_id"];
		$obj->task_start_date = date("Y-m-d h:i:s", time());
		$obj->task_end_date = date("Y-m-d h:i:s", time() + (24 * 60 * 60));
		$obj->task_description = $bug["description"];
		$obj->task_owner = $AppUI->user_id;
		$obj->task_milestone = false;
		$obj->task_type = 3;
		
		$result = createTask($obj);
		if(!$result){
			die("Task not added");
		}
		
		$mantis->resetRequest();
		$mantis->setFunction("MantisRPC");
		$mantis->addArg(array($mantis_user, $mantis_pass));
		$mantis->addArg("updateMantisBugStatusById");
		$mantis->addArg($b);
		$mantis->addArg("30");
	
		$result = $mantis->call();
	
		if(ERROR::isError($result)){
			die($result->getErrstr());
		}
		
	}
	
	$AppUI->redirect($forward_link);
	die();
	

}
else {	
	foreach($_POST["bugs"] as $b){
		$mantis->resetRequest();
		$mantis->setFunction("MantisRPC");
		$mantis->addArg(array($mantis_user, $mantis_pass));
		$mantis->addArg("updateMantisBugStatusById");
		$mantis->addArg($b);
		$mantis->addArg($_POST["bug_action"]);
	
		$result = $mantis->call();
	
		if(ERROR::isError($result)){
			die($result->getErrstr());
		}
	}
	
	$AppUI->redirect($forward_link);
	die();
	
}

function createTask($obj){
	
	// Include any files for handling module-specific requirements
	foreach (findTabModules('tasks', 'addedit') as $mod) {
		$fname = dPgetConfig('root_dir') . "/modules/$mod/tasks_dosql.addedit.php";
		dprint(__FILE__, __LINE__, 3, "checking for $fname");
		if (file_exists($fname))
			require_once $fname;
	}	

	// If we have an array of pre_save functions, perform them in turn.
	if (isset($pre_save)) {
		foreach ($pre_save as $pre_save_function)
			$pre_save_function();
	} else {
		dprint(__FILE__, __LINE__, 1, "No pre_save functions.");
	}
	
	$msg = $obj->store();
	
	if($msg){
		return false;
	}
	
	if (isset($post_save)) {
		foreach ($post_save as $post_save_function) {
			$post_save_function();
		}
	}

	if ($notify) {
		if ($msg = $obj->notify($comment)) {
			$AppUI->setMsg( $msg, UI_MSG_ERROR );
		}
	}
	
	return true;
}

?>